package com.zj.system.shiro;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.zj.system.mapper.LoginMapper;
import com.zj.system.pojo.entity.User;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.AuthenticationInfo;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authc.SimpleAuthenticationInfo;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.apache.shiro.util.ByteSource;
import org.springframework.beans.factory.annotation.Autowired;

/**
 * @program: clms-seventh
 * @description: reaml
 * @author: 张静
 * @create: 2021-07-16 20:15
 **/

public class MyRealm extends AuthorizingRealm {
    @Autowired
    private LoginMapper loginMapper;
    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {
        return null;
    }

    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {
        String username=token.getPrincipal().toString();
        QueryWrapper wrapper=new QueryWrapper<>();
        wrapper.eq("status",0);
        wrapper.eq("user_name",username);
        User user =loginMapper.selectOne(wrapper);
        System.out.println(user);
        if (user!=null){
            ByteSource byteSource=ByteSource.Util.bytes(user.getSalt());
            SimpleAuthenticationInfo info=new SimpleAuthenticationInfo(user,user.getPassword(),byteSource,this.getName());
            return info;
        }
        return null;
    }
}
